package cao.oschina.function.me;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import cao.oschina.R;
import cao.oschina.function.FunctionFragment;
import cao.oschina.function.me.notice.MyNotice;
import cao.oschina.function.other.LoginHelper;
import cao.oschina.bean.User;
import cao.oschina.ui.Main;
import cao.oschina.util.FileUtils;
import hd.util.load.ImageLoader;

/**
 * 我
 * <p/>
 * Created by Haidy on 15/4/3.
 */
public class MeFragment extends FunctionFragment {

    private Bitmap headImage;

    private ImageView headView;
    private ImageView sexView;
    private TextView userNameView;
    private TextView scoreView;
    private TextView favoriteView;
    private TextView followersView;
    private TextView fansView;
    private View functionView;

    public MeFragment(Main main) {
        super(main);
    }

    /**
     * 要创建的视图
     *
     * @return view
     */
    @Override
    protected int createView() {
        return R.layout.me_view;
    }

    /**
     * 初始化界面
     *
     * @param view view
     */
    @Override
    protected void initView(View view) {
        headView = (ImageView) view.findViewById(R.id.icon);
        sexView = (ImageView) view.findViewById(R.id.sex);
        userNameView = (TextView) view.findViewById(R.id.userName);
        scoreView = (TextView) view.findViewById(R.id.score);
        favoriteView = (TextView) view.findViewById(R.id.favorite);
        followersView = (TextView) view.findViewById(R.id.followers);
        fansView = (TextView) view.findViewById(R.id.fans);
        functionView = view.findViewById(R.id.me_function);

        headView.setOnClickListener(clickListener);
        scoreView.setOnClickListener(clickListener);
        favoriteView.setOnClickListener(clickListener);
        followersView.setOnClickListener(clickListener);
        fansView.setOnClickListener(clickListener);
        view.findViewById(R.id.dimension).setOnClickListener(clickListener);
        view.findViewById(R.id.message).setOnClickListener(clickListener);
        view.findViewById(R.id.blog).setOnClickListener(clickListener);
        view.findViewById(R.id.note).setOnClickListener(clickListener);
        view.findViewById(R.id.group).setOnClickListener(clickListener);
    }

    /**
     * 初始化数据
     */
    @Override
    protected void initData() {
        setUserInfo();
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.icon:
                    userInfo();
                    break;
                case R.id.dimension:

                    break;
                case R.id.score:

                    break;
                case R.id.favorite:

                    break;
                case R.id.followers:

                    break;
                case R.id.fans:

                    break;
                case R.id.message:
                    openMessage();
                    break;
                case R.id.blog:

                    break;
                case R.id.note:

                    break;
                case R.id.group:

                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 打开消息 notice
     */
    private void openMessage() {
        startActivity(new Intent(main, MyNotice.class));
        main.activityPushAnim();
    }

    /**
     * 查看个人资料
     */
    private void userInfo() {
        if (app.getConfig().isLogin()) {
            startActivity(new Intent(getActivity(), MyInformation.class));
            main.activityPushAnim();
        } else {
            LoginHelper.remindLogin(getActivity(), onLoginResultListener);
        }
    }

    /**
     * 登录回调接口
     */
    private LoginHelper.OnLoginResultListener onLoginResultListener = new LoginHelper.OnLoginResultListener() {
        @Override
        public void onLoginResult(boolean result) {
            if (result) {
                setUserInfo();
            }
        }
    };

    /**
     * 显示用户信息
     */
    private void setUserInfo() {
        User user = app.getConfig().getUserInfo();
        if (user != null) {
            userNameView.setText(user.name);
            scoreView.setText(user.score + "\n积分");
            favoriteView.setText(user.favoritecount + "\n收藏");
            followersView.setText(user.followers + "\n关注");
            fansView.setText(user.fans + "\n粉丝");
            if ("1".equals(user.gender)) {
                sexView.setImageResource(R.mipmap.boy);
            } else {
                sexView.setImageResource(R.mipmap.girl);
            }
            if (user.portrait != null) {
                ImageLoader.setOnSingleImageLoaderListener(user.portrait, FileUtils.getHeadImagePath(getActivity(), user.portrait), new ImageLoader.OnSingleImageLoaderListener() {
                    @Override
                    public void onLoaded(Bitmap bitmap) {
                        recycleBitmap();
                        if (bitmap != null && !bitmap.isRecycled()) {
                            headView.setImageBitmap(headImage = bitmap);
                        } else {
                            headView.setImageResource(R.mipmap.ic_account_circle_grey600_36dp);
                        }
                    }
                });
            } else {
                headView.setImageResource(R.mipmap.ic_account_circle_grey600_36dp);
            }
            functionView.setVisibility(View.VISIBLE);
        } else {
            functionView.setVisibility(View.GONE);
        }
    }

    /**
     * 回收位图
     */
    private void recycleBitmap() {
        if (headImage != null && !headImage.isRecycled()) {
            headImage.recycle();
        }
    }

    @Override
    public void onDestroy() {
        recycleBitmap();
        super.onDestroy();
    }
}
